package edu.caece.langprocessor.syntax.parse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.caece.langprocessor.syntax.NonTerminal;

public class GotoActionsTable {
	private static final String SPLIT_STR = "~";	
	private Map<Integer, Map<NonTerminal, Integer>> gotoActionsMap;
	
	public GotoActionsTable(){
		this.gotoActionsMap = new HashMap<Integer, Map<NonTerminal, Integer>>();
	}
	
	public Integer getState(Integer state, NonTerminal nonTerminal){
		Integer i = null;
		if (this.gotoActionsMap.keySet().contains(state))
			i = this.gotoActionsMap.get(state).get(nonTerminal);
		return i;
	}
	
	public void setGotoActionsMapFromStringsList(List<String> list) {
		String[] actionArray;

		for (String item : list) {
			actionArray = item.split(SPLIT_STR);

			// primero tomamos el estado
			Integer state = Integer.parseInt(actionArray[0]);
			// Se setea el no terminal
			NonTerminal nt = new NonTerminal(actionArray[1]);
			// Se define el estado nuevo
			Integer newState = Integer.parseInt(actionArray[2]);

			// colocamos los datos en el mapa
			if (!this.gotoActionsMap.containsKey(state)) {
				this.gotoActionsMap.put(state,
						new HashMap<NonTerminal, Integer>());
			}
			this.gotoActionsMap.get(state).put(nt, newState);
		}
	}

}
